package me.cloudcat.develop.service.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import me.cloudcat.develop.dao.configuration.UserDao;
import me.cloudcat.develop.entity.configuration.User;
import me.cloudcat.develop.utils.StringUtils;

@Service
@Transactional(readOnly = true)
public class UserService {

	@Autowired
	UserDao userDao;

	public User findOne(Long id) {
		return userDao.findOne(id);
	}

	@Transactional(readOnly = false)
	public void remove(Long id) {
		userDao.delete(id);
	}

	/**
	 * 新增用户，密码加密
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void saveNew(User entity) {
		String md5 = StringUtils.md5(entity.getPassword());
		entity.setPassword(md5);
		userDao.save(entity);
	}

	/**
	 * 保存用户，密码不变
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void save(User entity) {
		userDao.save(entity);
	}

	/**
	 * 登录
	 * @param username
	 * @param pass
	 * @return
	 */
	public User login(String username, String pass) {
		String md5 = StringUtils.md5(pass);
		User user = userDao.findOne(username, md5);
		return user;

	}

}
